//#include "loadsprite.h"
#include "GameCore.h"
#include "GameEditor.h"

using namespace sleek;
using namespace core;
using namespace device;

namespace interne
{
    GameEditor::GameEditor(GameCore *co, core::engine::world* eng) : core(co), screen(eng->getDevice()), smgr(eng)
    {
        smgr->lock();

        smgr->getEngine()->getCamera()->setRotation(core::math::vector3df(0, 1, 0));
        smgr->getEngine()->getCamera()->setTarget(core::math::vector3df(0, 0, 0));
        smgr->getEngine()->getCamera()->setPosition(core::math::vector3df(0, 0, 100000));
        smgr->getEngine()->getCamera()->update();
        smgr->getDevice()->testGlError();

        screen->setEventReceiver(this);

        load("level/sample.json");
        smgr->unlock();
    }
    GameEditor::~GameEditor()
    {
        smgr->getEngine()->clear();
    }
    void GameEditor::save(const char *filename)
    {
        std::ofstream ofs;
        ofs.open(filename, std::ios::out);

        Json::StyledStreamWriter writer("   ");
        Json::Value worldValue;

        unsigned int i = 0;
        for (b2Body* b = core->phy->world->GetBodyList(); b; b = b->GetNext())
        {
            if(b->GetUserData() != NULL)
            {
                Entity *tmp = (Entity*)b->GetUserData();
                worldValue["body"][i] = tmp->save();
                i++;
            }
        }
        i = 0;
        for (b2Joint* joint = core->phy->world->GetJointList(); joint; joint = joint->GetNext()) {
            if(joint->GetType() == e_gearJoint) continue;
            worldValue["joint"][i] = loader.b2j(joint);
            i++;
        }
        i = 0;
        for (b2Joint* joint = core->phy->world->GetJointList(); joint; joint = joint->GetNext()) {
            if (joint->GetType() != e_gearJoint) continue;
            worldValue["joint"][i] = loader.b2j(joint);
            i++;
        }

        writer.write(ofs, worldValue);
        ofs.close();
    }
    void GameEditor::load(const char *filename)
    {
        smgr->getEngine()->clear();
        std::ifstream ifs;
        ifs.open(filename, std::ios::in);

        Json::Value worldValue;
        Json::Reader reader;
        reader.parse(ifs, worldValue);
        ifs.close();

        int i = 0;
        Json::Value bodyValue = worldValue["body"][i++];
        while ( !bodyValue.isNull() ) {
            loadEntity(bodyValue);
            bodyValue = worldValue["body"][i++];
        }
        i = 0;
        Json::Value jointValue = worldValue["joint"][i++];
        while(!jointValue.isNull()) {
            if ( jointValue["type"].asString() != "gear" ) {
                b2Joint* joint = loader.j2b2Joint(core->phy->world, jointValue);
            }
            jointValue = worldValue["joint"][i++];
        }
        i = 0;
        jointValue = worldValue["joint"][i++];
        while ( !jointValue.isNull() ) {
            if ( jointValue["type"].asString() == "gear" ) {
                b2Joint* joint = loader.j2b2Joint(core->phy->world, jointValue);
            }
            jointValue = worldValue["joint"][i++];
        }
    }
    void GameEditor::loadEntity(Json::Value value)
    {
        Entity *tmp;
        printf("%s\n", value["path"].asCString());
        switch(value["entity"].asInt())
        {
            case EET_ENTITY:
                tmp = new Entity(smgr->getEngine(), value["path"].asCString());
            break;
            case EET_PLAYER:
                tmp = new Player(smgr->getEngine());
            break;
            case EET_SPRITE:
                tmp = new Sprite(smgr->getEngine());
            break;
            case EET_WARP:
                tmp = new Warp(smgr->getEngine());
            break;
        }
        tmp->env->setPosition(sleek::core::math::vector3df(0,0,value["zorder"].asFloat()));
        tmp->env->setScale(sleek::core::math::vector3df(value["scale_x"].asFloat(),value["scale_y"].asFloat(),value["scale_z"].asFloat()));
        tmp->phy = loader.j2b2Body(core->phy->world, value);

        tmp->phy->SetUserData(tmp);
        smgr->getEngine()->addSceneNode(tmp->env);
    }
    bool GameEditor::manage(sleek::core::device::input *a)
    {
        return event::manage(a);
    }
    void GameEditor::render()
    {
    }
}
